package com.example.bidserver.service;

import com.example.bidserver.entity.Bid;
import com.example.bidserver.manager.BidManager;
import com.example.bidserver.params.BidParams;
import com.example.bidserver.params.PageBidParams;
import com.example.bidserver.vo.BidVO;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.utils.BeanCopyUtils;
import com.example.userserver.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BidService {
    @Autowired
    private BidManager bidManager;
    public PageInfo<BidVO> pageByUserId(PageBidParams pageBidParams){
        PageInfo<BidVO> pageInfo = PageHelper.startPage(pageBidParams)
                .doSelectPageInfo(() ->  bidManager.listBidByUserId(pageBidParams));
        return pageInfo;
    }
    public PageInfo<BidVO> pageByPostId(PageBidParams pageBidParams){
        PageInfo<BidVO> pageInfo = PageHelper.startPage(pageBidParams)
                .doSelectPageInfo(() ->  bidManager.listBidByPostId(pageBidParams));
        return pageInfo;
    }
    public PageInfo<BidVO> pageByGoodsId(PageBidParams pageBidParams){
        PageInfo<BidVO> pageInfo = PageHelper.startPage(pageBidParams)
                .doSelectPageInfo(() ->  bidManager.listBidByGoodsId(pageBidParams));
        return pageInfo;
    }

    public void deleteBid(String id) {
        bidManager.deleteBid(id);
    }

    public Result updateBid(BidParams bidParams) {
        Bid bid = bidManager.getById(bidParams.getId());
        Bid bidUpdate = new Bid();
        BeanCopyUtils.copyPropertiesIgnoreNull(bidParams, bidUpdate);
        if(bidUpdate.getPrice() == 0){
            bidUpdate.setPrice(bid.getPrice());
        }
        bidManager.updateBid(bidUpdate);
        return new Result(null, ReturnEnum.SUCCESS);
    }

    public Result insertBid(BidParams bidParams) {
        Bid bid = new Bid();
        BeanCopyUtils.copyPropertiesIgnoreNull(bidParams, bid);
        bidManager.insertBid(bid);
        return new Result(null, ReturnEnum.SUCCESS);
    }
}
